﻿using System;
using System.Linq;
using System.Web;
using WEIShareWCFRole.BaseCode;
using WEIShareWCFRole.BusinessEntities;
using WEIShareWCFRole.BusinessEntities.Paging;

namespace WEIShareWCFRole.BusinessLogicLayer
{
    /// <summary>
    /// Summary description for WindowsAssessment
    /// </summary>
    public class WindowsAssessment
    {

        public static BusinessEntities.WindowsAssessment GetByID(Guid ID)
        {
            PageRequest pageRequest = new PageRequest();
            pageRequest.OrderBy = "";
            pageRequest.OrderByDirection = "";
            pageRequest.SearchQuery = "";
            pageRequest.PageNumber = 1;
            pageRequest.PageSize = 1;
            pageRequest.Filter = new Filter() { Parameter = "WindowsAssessmentID", Value = ID.ToString() };

            PageResponse<BusinessEntities.WindowsAssessment> pagedEntities = GetPaged(pageRequest);

            BusinessEntities.WindowsAssessment entity = null;
            if (pagedEntities != null & pagedEntities.PageListing.Count() > 0)
                entity = pagedEntities.PageListing.FirstOrDefault();

            return entity;
        }


        public static PageResponse<BusinessEntities.WindowsAssessment> GetPaged(PageRequest pageRequest)
        {
            return GetPaged(pageRequest, null);
        }

        public static PageResponse<BusinessEntities.WindowsAssessment> GetPaged(PageRequest pageRequest, AdvancedSearch advancedSearch)
        {
            PageResponse<BusinessEntities.WindowsAssessment> pagedResponse = new PageResponse<BusinessEntities.WindowsAssessment>();

            string filterParameter = "";
            string filterValue = "";

            if (pageRequest.Filter != null)
            {
                filterParameter = pageRequest.Filter.Parameter;
                filterValue = pageRequest.Filter.Value;
            }

            string key = "WindowsAssessment_GetPaged_OrderBy:{0}_OrderByDirection:{1}_PageNumber:{2}_PageSize:{3}_SearchQuery:{4}_FilterParameter:{5}_FilterValue:{6}_GroupResults:{7}_Admin:{8}";
            key = String.Format(key, pageRequest.OrderBy, pageRequest.OrderByDirection, pageRequest.PageNumber, pageRequest.PageSize, pageRequest.SearchQuery, filterParameter, filterValue, pageRequest.GroupResults, pageRequest.IsAdmin);

            if (advancedSearch != null)
            {
                key += "_SystemScoreMin:{0}_SystemScoreMax:{1}_WindowsVersion:{2}_ProcessorName:{3}:_ProcessorNumCoresMin:{4}_ProcessorNumCores:{5}_ProcessorL1CacheSizeMin:{6}_ProcessorL2CacheSizeMax:{7}";
                key = String.Format(key, advancedSearch.SystemScoreMin, advancedSearch.SystemScoreMax, advancedSearch.WindowsVersion, advancedSearch.ProcessorName, advancedSearch.ProcessorNumCoresMin, advancedSearch.ProcessorNumCoresMax, advancedSearch.ProcessorL2CacheSizeMin, advancedSearch.ProcessorL2CacheSizeMax);

                key += "_MemoryTypeID:{0}_MemoryPhysicalSizeMin:{1}_MemoryPhysicalSizeMax:{2}_MemorySpeedMin:{3}_MemorySpeedMax:{4}_GraphicsAdapterDescription:{5}_GraphicsMemoryMin:{6}_GraphicsMemoryMax:{7}";
                key = String.Format(key, advancedSearch.MemoryTypeID, advancedSearch.MemoryPhysicalSizeMin, advancedSearch.MemoryPhysicalSizeMax, advancedSearch.MemorySpeedMin, advancedSearch.MemorySpeedMax, advancedSearch.GraphicsAdapterDescription, advancedSearch.GraphicsMemoryMin, advancedSearch.GraphicsMemoryMax);

                key += "_MonitorsCountMin:{0}_MonitorsCountMax:{1}_DiskSizeMin:{2}_DiskSizeMax:{3}";
                key = String.Format(key, advancedSearch.MonitorsCountMin, advancedSearch.MonitorsCountMax, advancedSearch.DiskSizeMin, advancedSearch.DiskSizeMax);
            }

            PageResponse<BusinessEntities.WindowsAssessment> cachedList = null;

            if (HttpRuntime.Cache != null) cachedList = HttpRuntime.Cache.Get(key) as PageResponse<BusinessEntities.WindowsAssessment>;

            if (cachedList != null)
            {
                pagedResponse = cachedList;
                pagedResponse.IsCached = true;
            }
            else
            {
                pagedResponse.IsCached = false;
                pagedResponse = DataAccessLayer.WindowsAssessment.GetPaged(pageRequest, advancedSearch);

                if (pagedResponse != null && HttpRuntime.Cache != null)
                    HttpRuntime.Cache.Insert(key, pagedResponse, null, DateTime.Now.AddSeconds(300), TimeSpan.Zero);
            }

            return pagedResponse;
        }

        public static Boolean WindowsAssessementAlreadyRecorded(BusinessEntities.WindowsAssessment windowsAssessmentEntity)
        {
            bool assessmentAlreadyRecorded = false;

            string key = "WindowsAssessment_WindowsAssessementAlreadyRecorded_MacAddress:{0}_SystemScore:{1}_MemoryScore:{2}_CpuScore:{3}_GraphicsScore:{4}_GamingScore:{5}_DiskScore:{6}";
            key = String.Format(key, windowsAssessmentEntity.MACAddress, windowsAssessmentEntity.SystemScore, windowsAssessmentEntity.MemoryScore, windowsAssessmentEntity.CpuScore, windowsAssessmentEntity.GraphicsScore, windowsAssessmentEntity.GamingScore, windowsAssessmentEntity.DiskScore);

            bool? cachedResponse = null;

            if (HttpRuntime.Cache != null) cachedResponse = HttpRuntime.Cache.Get(key) as bool?;

            if (cachedResponse.HasValue)
            {
                assessmentAlreadyRecorded = cachedResponse.Value;
            }
            else
            {
                assessmentAlreadyRecorded = DataAccessLayer.WindowsAssessment.WindowsAssessementAlreadyRecorded(windowsAssessmentEntity);

                if (assessmentAlreadyRecorded != null && HttpRuntime.Cache != null)
                    HttpRuntime.Cache.Insert(key, assessmentAlreadyRecorded, null, DateTime.Now.AddSeconds(300), TimeSpan.Zero);
            }

            return assessmentAlreadyRecorded;
        }

        public static Guid Save(BusinessEntities.WindowsAssessment windowsAssessmentEntity)
        {
            Helpers.PurgeCacheItems("WindowsAssessment");

            return DataAccessLayer.WindowsAssessment.Save(windowsAssessmentEntity);
        }

        public static void Delete(Guid ID)
        {
            Helpers.PurgeCacheItems("WindowsAssessment");

            DataAccessLayer.WindowsAssessment.Delete(ID);
        }

    }
}